/**
 * TmCustomFactoryServiceImpl.java
 * Created at 2017-3-1
 * Created by zhangzhiqiang002
 * Copyright (C) 2017 BROADTEXT SOFTWARE, All rights reserved.
 */
package com.broadtext.rms.basic.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.broadtext.common.exp.DaoException;
import com.broadtext.common.repository.EntityDao;
import com.broadtext.common.service.impl.EntityServiceImpl;
import com.broadtext.common.utils.RmsUtil;
import com.broadtext.common.utils.UUIDUtils;
import com.broadtext.rms.basic.model.TmCustomFactory;
import com.broadtext.rms.basic.model.TmEmploy;
import com.broadtext.rms.basic.model.TrPackCustomFactory;
import com.broadtext.rms.basic.service.ITmCustomFactoryService;


/**
 * <p>ClassName: TmCustomFactoryServiceImpl</p>
 * <p>Description: 客户厂区服务层</p>
 * <p>Author: zhangzhiqiang002</p>
 * <p>Date: 2017-3-1</p>
 */
@Service("tmCustomFactoryService")
public class TmCustomFactoryServiceImpl extends EntityServiceImpl<TmCustomFactory> implements ITmCustomFactoryService {
    @Autowired
    @Qualifier("tmCustomFactoryDao")
    public void setEntityDao(EntityDao<TmCustomFactory> entityDao) {
        super.entityDao = entityDao;
    }
    
    /**
     * Mapper请求前缀
     */
    private final String modelName = "com.broadtext.rms.basic.model.TmCustomFactory";
    
    /**
     * 
     * <p>Description: 批量删除 </p>
     * @param idArray 主键数组
     * @param updateUser 修改人姓名
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public String delete(String[] idArray, String updateUser) throws DaoException {
        for (String id : idArray) {
            List<TrPackCustomFactory> list = new ArrayList<TrPackCustomFactory>();
            list = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TrPackCustomFactory.findEntityListByFactoryId", id);
            List<TmEmploy> empList = new ArrayList<TmEmploy>();
            empList = this.entityDao.findByProperty("com.broadtext.rms.basic.model.TmEmploy.findContactsByFactory", id);
            if (list != null && list.size() > 0) {
                return "packFail";
            } else if (empList != null && empList.size() > 0) {
                return "employeeFail";
            } else{
                TmCustomFactory t;
                t = new TmCustomFactory();
                t.setPkId(id);
                t.setUpdateDate(new Date());
                t.setUpdateUser(updateUser);
                t.setDelFlag("1");
                this.entityDao.updateRecord(this.modelName + ".deleteRecord", t);
            }
        }
        return "success";
    }

    /**
     * 
     * <p>Description: 修改厂区</p>
     * @param factory 厂区对象
     * @param loginName 登录用户名
     * @throws DaoException 异常
     */
    @Override
    public void updateCustomFactory(TmCustomFactory factory, String loginName) throws DaoException {
        TmCustomFactory oldCustomFactory = new TmCustomFactory();
        String id = factory.getPkId();
        oldCustomFactory = this.entityDao.findEntityById(this.modelName + ".selectByPrimaryKey", id);
        oldCustomFactory.setCustomId(factory.getCustomId());
        oldCustomFactory.setFacName(factory.getFacName());
        oldCustomFactory.setProvince(factory.getProvince());
        oldCustomFactory.setCity(factory.getCity());
        oldCustomFactory.setArea(factory.getArea());
        oldCustomFactory.setAddress(factory.getAddress());
        oldCustomFactory.setZipCode(factory.getZipCode());
        oldCustomFactory.setUpdateDate(new Date());
        oldCustomFactory.setUpdateUser(loginName);
        this.entityDao.updateRecord(this.modelName + ".updateRecord", oldCustomFactory);
    }

    /**
     * 
     * <p>Description: 新增厂区</p>
     * @param factory 厂区对象
     * @param loginName 登录用户名
     * @throws DaoException 异常
     */
    @Override
    public void insertCustomFactory(TmCustomFactory factory, String loginName) throws DaoException {
    	factory.setPkId(UUIDUtils.getUUID());
        factory.setCreateDate(new Date());
        factory.setCreateUser(loginName);
        factory.setUpdateDate(new Date());
        factory.setUpdateUser(loginName);
        factory.setDelFlag("0");
        this.entityDao.insertRecord(this.modelName + ".insertRecord", factory);
    }

    /**
     * <p>Description: 根据id获取厂区</p>
     * @param id 厂区主键id
     * @return 厂区对象
     * @throws DaoException 异常
     */
    @Override
    public TmCustomFactory getCustomFactoryById(String id) throws DaoException {
        TmCustomFactory tmCustomFactory = new TmCustomFactory();
        tmCustomFactory = this.entityDao.findEntityById(this.modelName + ".selectByPrimaryKey", id);
        return tmCustomFactory;
    }

    /**
     * <p>Description: 根据id获取厂区详情</p>
     * @param id 厂区主键id
     * @return 厂区对象
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> getCustomFactoryDetail(String id) throws DaoException {
        return (Map<String, Object>)this.entityDao.findById(this.modelName + ".findEntityDetailById", id);
    }

    /**
     * <p>Description: 初始化电池关联绑定页面</p>
     * @param id 厂区主键id
     * @return 绑定页面结果集
     * @throws DaoException 异常
     */
    @SuppressWarnings("unchecked")
    @Override
    public Map<String, Object> initBatteryCorrelation(String id) throws DaoException {
        Map<String, Object> map = new HashMap<String, Object>();
        Map<String, Object> factory = new HashMap<String, Object>();
        factory = (Map<String, Object>)this.entityDao.findById(this.modelName + ".findBatteryCorrelationEntity", id);
        if (!RmsUtil.isBlank(factory.get("pkId"))) {
            Map<String, Object> queryMap = new HashMap<String, Object>();
            queryMap.put("pkId", factory.get("pkId"));
            queryMap.put("customId", factory.get("customId"));
            map.put("factory", factory);
            map.put("multiSelect", this.entityDao.findByQuery(this.modelName + ".findBatteryCorrelationMutileSelect", queryMap));
        }
        return map;
    }

    /**
     * <p>Description: 电池关联绑定</p>
     * @param tmPackCustomFactory 电池于客户厂区关联对象
     * @param loginUser 当前登录用户名
     * @throws DaoException 异常
     */
    @Override
    public void saveBatteryCorrelation(TrPackCustomFactory tmPackCustomFactory, String loginUser) throws DaoException {
        String[] idArray;
        idArray = new String[]{};
        String packCodes;
        packCodes = tmPackCustomFactory.getPackCode();
        if (!"".equals(packCodes)) {
            idArray = packCodes.split(",");
        }
        TrPackCustomFactory packCustomFactory = new TrPackCustomFactory();
        packCustomFactory.setOwnerComp(tmPackCustomFactory.getOwnerComp());
        packCustomFactory.setOwnerFactory(tmPackCustomFactory.getOwnerFactory());
        packCustomFactory.setDelFlag("0");
        packCustomFactory.setCreateDate(new Date());
        packCustomFactory.setCreateUser(loginUser);
        packCustomFactory.setUpdateDate(new Date());
        packCustomFactory.setUpdateUser(loginUser);
        Map<String, Object> queryMap = new HashMap<String, Object>();
        queryMap.put("ownerComp", tmPackCustomFactory.getOwnerComp());
        queryMap.put("ownerFactory", tmPackCustomFactory.getOwnerFactory());
        this.entityDao.findByQuery("com.broadtext.rms.basic.model.TrPackCustomFactory.deleteRecord", queryMap);
        for (String packCode : idArray) {
            packCustomFactory.setPkId(UUIDUtils.getUUID());
            packCustomFactory.setPackCode(packCode);
            this.entityDao.insertRecord("com.broadtext.rms.basic.model.TrPackCustomFactory.insertRecord", packCustomFactory);
        }
    }
}
